package com.wt.wechatTools.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wt.wechatTools.R;
import com.wt.wechatTools.room.WorkRepository;
import com.wt.wechatTools.room.entity.ContactsEntity;

import java.util.ArrayList;
import java.util.List;

public class AdapterContacts extends RecyclerView.Adapter<AdapterContacts.MyViewHolder> {
    private static final String TAG = "AdapterContacts";
    private List<ContactsEntity> contactsEntities = new ArrayList<>();
    private WorkRepository workRepository;

    public AdapterContacts(Context context) {
        this.workRepository = new WorkRepository(context);
    }

    /**
     * 设置数据
     *
     * @param contactsEntities
     */
    public void setContactsEntities(List<ContactsEntity> contactsEntities) {
        this.contactsEntities = contactsEntities;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView txtPhone;
        TextView txtName;
        Button btnContactsItem;
        CheckBox cbContactsItem;

        public MyViewHolder(View iteView) {
            super(iteView);
            this.txtPhone = iteView.findViewById(R.id.txtContactsPhone);
            this.btnContactsItem = iteView.findViewById(R.id.btnContactsItem);
            this.cbContactsItem = iteView.findViewById(R.id.cbContactsItem);
            this.txtName = iteView.findViewById(R.id.txtContactsName);
        }
    }

//        spinnerList.add("任务清单");//等于5为 "任务清单"
//        spinnerList.add("最近任务");//tastBatch任务批次为最大的数据
//        spinnerList.add("未使用");//等于1为"未使用"
//        spinnerList.add("执行成功");//等于2为"执行成功"
//        spinnerList.add("执行失败");//等于3为"执行失败"
//        spinnerList.add("添加成功");//等于4为 "添加成功"

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ContactsEntity contact = contactsEntities.get(position);
        holder.txtName.setText(contact.getName());
        holder.txtPhone.setText(String.valueOf(contact.getCellPhone()));
        switch (contact.getState()) {
            case 5:
                holder.btnContactsItem.setText("移出清单");
                holder.cbContactsItem.setVisibility(View.INVISIBLE);
                holder.btnContactsItem.setEnabled(true);
                break;
            case 1:
                holder.btnContactsItem.setText("加入任务");
                holder.cbContactsItem.setVisibility(View.VISIBLE);
                holder.btnContactsItem.setEnabled(true);
                break;
            case 2:
                holder.cbContactsItem.setVisibility(View.VISIBLE);
                holder.btnContactsItem.setText("加入任务");
                holder.btnContactsItem.setEnabled(true);
                break;
            case 3:
//                该用户不存在  被搜帐号状态异常，无法显示
                holder.cbContactsItem.setVisibility(View.INVISIBLE);
                holder.btnContactsItem.setText("删除");
                holder.btnContactsItem.setEnabled(true);
                break;
            case 4:
                holder.cbContactsItem.setVisibility(View.INVISIBLE);
                holder.btnContactsItem.setText("添加成功");
                holder.btnContactsItem.setEnabled(false);
                break;
        }

        holder.btnContactsItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (contact.getState()) {
                    case 5:
//                        holder.btnContactsItem.setText("移出清单");
//                        holder.cbContactsItem.setVisibility(View.INVISIBLE);
                        contact.setState(1);
                        workRepository.updateContact(contact);
                        contactsEntities.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, contactsEntities.size() - position);
                        break;
                    case 1:
                        contact.setState(5);
                        workRepository.updateContact(contact);
                        contactsEntities.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, contactsEntities.size() - position);
                        break;
                    case 2:
                        contact.setState(5);
                        workRepository.updateContact(contact);
                        contactsEntities.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, contactsEntities.size() - position);
                        break;
                    case 3:
//                该用户不存在  被搜帐号状态异常，无法显示
//                holder.cbContactsItem.setVisibility(View.INVISIBLE);
//                holder.btnContactsItem.setText("删除");
                        workRepository.deleteContact(contact);
                        contact.setState(5);
                        contactsEntities.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, contactsEntities.size() - position);
                        break;
                }
            }
        });

        //列单击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }


    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View iteView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_contacts_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(iteView);
        return viewHolder;
    }

    @Override
    public int getItemCount() {
        return contactsEntities.size();
    }
}
